package com.skyolder.taskman.ast;

import java.util.Iterator;

import com.skyolder.taskman.Container;
import com.skyolder.taskman.tool.TimeTool;
import com.skyolder.taskman.word.Token;
import com.skyolder.taskman.word.TokenType;

public class DELAY_Branch extends Branch {
	public DELAY_Branch(Container c) {
		super(c);
	}

	Long delayTime=0L;
	Tree tr=null;
	@Override
	public void build(Iterator<Token> it) throws GrammarError, FormatError {
		Token token=it.next();
		if(token==null){
			throw new GrammarError(Token.NULL);
		}
		TokenType tp=token.getTp();
		if(tp!=TokenType.PARAM){
			throw new GrammarError(token);
		}
		delayTime=TimeTool.parseTime(token.getWord());
		tr=AstBuilder.buildFromTokens(it,this.getContainer());
		if(tr==null){
			throw new GrammarError(Token.NULL);
		}
	}

	@Override
	public Object excute() throws RunError  {
		try {
			Thread.sleep(delayTime);
		} catch (InterruptedException e) {
			return e.getMessage();
		}
		return tr.excute();
	}

}
